﻿#region License statement
// NJamb, a specification and delta-specification DSL
// Copyright (c) 2010-2011, Mark Knell
// Published under the MIT License; all other rights reserved
#endregion

#region using...
using System;
using NJamb.NUnit;
using NJamb.Parser.SpecificationBuilders.SubjectIs.Extensions;
using NJamb.Parser.SubjectBuilders;
using NJamb.Parser.Subjects.Singular;
using NJamb.Specifications.Simple.Singular;
using NUnit.Framework;
using Stile.Patterns.SelfDescribingPredicates;
using Stile.Patterns.SelfDescribingPredicates.Evaluators;
using Stile.Readability;
using Stile.Testing.SampleTypes;
#endregion

namespace NJamb.Tests.Specifications.Simple.Singular
{
    [TestFixture]
    public class SingularAcceptanceTests : EvaluatingSampleFixture
    {
        [Test]
        public void HasHashCode()
        {
            // arrange
            ISingularSpecification<int> singularSpecification =
                Specify.For(() => _sample).That(x => x.Int).Has.HashCode(_sample.Int.GetHashCode());

            // act
            IEvaluation evaluation = singularSpecification.Evaluate();

            // assert
            Assert.IsNotNull(evaluation);
            Assert.That(evaluation.Success, Is.True);
        }

        [Test]
        public void IsEqualTo()
        {
            // arrange
            ISingularSpecification<int> specification = Specify.For(() => _sample).That(x => x.Int).Is.EqualTo(_sample.Int - 1);

            // act
            IEvaluation<int> evaluation = specification.Evaluate();

            // assert
            AssertEvaluationFailed(evaluation, "expected _sample.Int would be 0" + Environment.NewLine + " but was 1.");
        }

        [Test]
        public void NullableInt()
        {
            // arrange
            ISingularSpecification<int?> specification = Specify.For(() => _sample).That(x => x.NullableInt).Is.Not.Null();

            // act
            IEvaluation<int?> evaluation = specification.Evaluate();

            // assert
            AssertEvaluationFailed(evaluation,
                "expected _sample.NullableInt would not be <null>" + Environment.NewLine + " but was <null>.");
        }

        [Test]
        public void Satisfies()
        {
            // arrange
            ISingularSubject<int> singularSubject = Specify.For(() => _sample).That(x => x.Int);
            Evaluator<int, int> evaluator = Expect<int>.Satisfies(y => y != Sample.Default.Int);

            // act
            ISingularSpecification<int> specification = singularSubject.Satisfies(evaluator);

            // assert
            IEvaluation evaluation = specification.Evaluate();
            Assert.That(evaluation.Success, Is.False);
            Assert.That(evaluation.Reason,
                Is.EqualTo(
                    string.Format("expected _sample.Int would satisfy y => y != {0}" + Environment.NewLine + " but was {0}.",
                        Sample.Default.Int.ToDebugString())));
        }
    }
}
